package controller

import (
	"fmt"
	"fx_swift_ifcgate/api"
	"fx_swift_ifcgate/internal/service"
	"fx_swift_ifcgate/pkg/code"
	"fx_swift_ifcgate/pkg/internalerrors"
	"fx_swift_ifcgate/pkg/validator"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Register(c *gin.Context) {
	req := &api.RegisterReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, validator.GetErrorMsg(req, err))
		return
	}
	result, err := service.NewLoginSrv().Register(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

func Login(c *gin.Context) {
	req := &api.LoginReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, validator.GetErrorMsg(req, err))
		return
	}
	fmt.Println(c.Request.Header.Get("X-Forward-For"))
	result, err := service.NewLoginSrv().Login(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

func SignOut(c *gin.Context) {
	err := service.NewLoginSrv().SignOut(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

func SendMailCode(c *gin.Context) {
	email := c.Query("email")
	err := service.NewLoginSrv().SendMailCode(c, email)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

func ForgetPassword(c *gin.Context) {
	req := &api.ForgetPasswordReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, validator.GetErrorMsg(req, err))
		return
	}
	err := service.NewLoginSrv().ForgetPassword(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

func UpdatePassword(c *gin.Context) {
	req := &api.UpdatePasswordReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, validator.GetErrorMsg(req, err))
		return
	}
	err := service.NewLoginSrv().UpdatePassword(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

func GetAgentInfo(c *gin.Context) {
	result, err := service.NewLoginSrv().GetAgentInfo(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

func EmailIsExit(c *gin.Context) {
	email := c.Query("email")
	result, err := service.NewLoginSrv().EmailIsExit(c, email)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}
